package com.niit.xlztest.controller;

import com.niit.xlztest.bean.Book;
import com.niit.xlztest.bean.Shopping_cart;
import com.niit.xlztest.repository.BuyRepository;
import com.niit.xlztest.repository.DetailRepository;
import com.niit.xlztest.service.BookService;
import com.niit.xlztest.service.BuyService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

@Controller
public class BuyController {
    @Autowired
    private BuyRepository buyRepository;
    @Autowired
    private BuyService buyService;
    @Autowired
    private BookService bookService;
    @Autowired
    private DetailRepository detailRepository;
    @GetMapping("/goToHome")
    public String goToHome() {
        return "Home";
    }

    @PostMapping("/addBookInCart")
    @Transactional
    public String addBookInCart(@RequestParam(name = "book_id") Integer book_id, Model model, HttpServletRequest request ) {
        //加入购物车
        String book_id_s = String.valueOf(book_id);

        //构建购物车对象
        Integer user_id = 123;
        Shopping_cart shopping_cart = new Shopping_cart();
        shopping_cart.setBook_id(book_id);
        shopping_cart.setUser_id(user_id);
        //将购物车对象插入购物车内
        buyService.cart(shopping_cart);

        //查出刚刚的书让用户看起来没有离开当前页
        Book book = detailRepository.findBookBid(book_id_s);
        model.addAttribute("book", book);
        return "detail";
    }
    @PostMapping("/byBookInCart")
    @Transactional
    public String byBookInCart(@RequestParam(name = "book_id") Integer book_id, Model model, HttpServletRequest request ) {
//        购买功能
        Integer user_id = 123;
//        用于销量＋1
          buyRepository.buyBook(book_id);
//        删除购物车记录
          buyRepository.deleteByUserIdAndBookId(user_id,book_id);

        List<Shopping_cart> shoppingCarts = buyService.findAllBookInCart(user_id);
        List<String> bookNames = new ArrayList<>();
        List<String> bookPrices = new ArrayList<>();
        List<String> bookCovers = new ArrayList<>();
        for (Shopping_cart cart : shoppingCarts) {
            String book_id_f = String.valueOf(cart.getBook_id());
            Book book = bookService.findBookByid(book_id_f);
            String book_name = book.getBook_name();
            String book_price = String.valueOf(book.getBook_price());
            byte[] bookCover = book.getBook_cover();
            if (bookCover != null) { // 添加对 bookCover 是否为空的检测
                String bookCoverJpg = "data:image/jpeg;base64," + Base64.getEncoder().encodeToString(bookCover);
                book.setBookCoverJpg(bookCoverJpg);
            }
            String bookCoverJpg = book.getBookCoverJpg();
            bookNames.add(book_name);
            bookPrices.add(book_price);
            bookCovers.add(bookCoverJpg);
        }

        model.addAttribute("shoppingCarts", shoppingCarts);
        model.addAttribute("bookNames", bookNames);
        model.addAttribute("bookPrices", bookPrices);
        model.addAttribute("bookCovers", bookCovers);
        return "shoppingCarts";
    }

    @GetMapping("/findAllBookInCart")
    public String findAllBookInCart(Model model){
        Integer user_id = 123;
        List<Shopping_cart> shoppingCarts = buyService.findAllBookInCart(user_id);
        List<String> bookNames = new ArrayList<>();
        List<String> bookPrices = new ArrayList<>();
        List<String> bookCovers = new ArrayList<>();
        List<Integer> bookIds = new ArrayList<>();
        for (Shopping_cart cart : shoppingCarts) {
            String book_id_s = String.valueOf(cart.getBook_id());
            Book book = bookService.findBookByid(book_id_s);
            String book_name = book.getBook_name();
            String book_price = String.valueOf(book.getBook_price());
            byte[] bookCover = book.getBook_cover();
            if (bookCover != null) { // 添加对 bookCover 是否为空的检测
                String bookCoverJpg = "data:image/jpeg;base64," + Base64.getEncoder().encodeToString(bookCover);
                book.setBookCoverJpg(bookCoverJpg);
            }
            String bookCoverJpg = book.getBookCoverJpg();
            bookNames.add(book_name);
            bookPrices.add(book_price);
            bookCovers.add(bookCoverJpg);
            Integer book_id = cart.getBook_id();
            bookIds.add(book_id);
        }
//        Book book = bookService.findBookByid(book_id);
        model.addAttribute("shoppingCarts", shoppingCarts);
        model.addAttribute("bookNames", bookNames);
        model.addAttribute("bookPrices", bookPrices);
        model.addAttribute("bookCovers", bookCovers);
        model.addAttribute("bookIds", bookIds);
        return "shoppingCarts";
    }
}
